package com.dayouzi.crawler_monitor.utils;

import java.sql.Timestamp;
import java.time.*;
import java.time.format.DateTimeFormatter;

public class DateUtils {

    public static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    /**
     * 带时分秒的格式化，返回不带时分秒
     * @param dateString
     * @return
     */
    public static LocalDate parseByHour(String dateString){
        return LocalDate.parse(dateString, DATE_TIME_FORMATTER);
    }

    /**
     * 带时分秒的格式化，返回带时分秒
     * @param dateString
     * @return
     */
    public static LocalDateTime parseByHour1(String dateString){
        return LocalDateTime.parse(dateString, DATE_TIME_FORMATTER);
    }

    /**
     * 不带时分秒的格式化，返回不带时分秒
     * @param dateString
     * @return
     */
    public static LocalDate parse(String dateString){
        return LocalDate.parse(dateString, DATE_FORMATTER);
    }

    /**
     * 带时分秒的格式化，返回字符串不带时分秒
     * @param localDate
     * @return
     */
    public static String formatByHour(LocalDate localDate) {
        return localDate.format(DATE_TIME_FORMATTER);
    }

    /**
     * 带时分秒的格式化，返回字符串带时分秒
     * @param localDateTime
     * @return
     */
    public static String formatByHour(LocalDateTime localDateTime) {
        return localDateTime.format(DATE_TIME_FORMATTER);
    }

    /**
     * 不带时分秒的格式化，返回字符串
     * @param localDate
     * @return
     */
    public static String format(LocalDate localDate) {
        return localDate.format(DATE_FORMATTER);
    }

    /**
     * 秒级时间戳转换成日期格式
     * @param timestamp
     * @return
     */
    public static LocalDateTime timestampToLocal(String timestamp) {
        Long time = Long.valueOf(timestamp);
        return LocalDateTime.ofInstant(Instant.ofEpochSecond(time), ZoneOffset.ofHours(8));
    }

    /**
     * 毫秒级时间戳转换成日期格式
     * @param timestamp
     * @return
     */
    public static LocalDateTime timestampToLocal2(String timestamp) {
        Long time = Long.valueOf(timestamp);
        return LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneOffset.ofHours(8));
    }

    /**
     * 获取当前时间戳
     * @return
     */
    public static long getTimestampNow() {
        // 获取当前时间的时间戳（以毫秒为单位）
        long currentTimeMillis = System.currentTimeMillis();
        // 将时间戳转换为 LocalDateTime 类型
        LocalDateTime localDateTime = LocalDateTime.ofInstant(
                Instant.ofEpochMilli(currentTimeMillis),
                ZoneId.systemDefault());
        // 将 LocalDateTime 类型的时间戳转换为 Timestamp 类型
        Timestamp timestamp = Timestamp.valueOf(localDateTime);

        // 输出类似于 1682081916 格式的时间戳
        return timestamp.getTime() / 1000;
    }

    /**
     * 将 ISO 8601 ，即 UTC 时区的时间，转换为北京时间
     * @param isoDate
     * @return
     */
    public static String getISOTime(String isoDate) {
        // 将字符串类型转换成日期类型
        LocalDateTime dateTime = LocalDateTime.parse(isoDate, DateTimeFormatter.ISO_OFFSET_DATE_TIME);

        // 获取北京时区
        ZoneId beijingZone = ZoneId.of("Asia/Shanghai");
        ZonedDateTime beijingTime  = dateTime.atOffset(ZoneOffset.UTC).atZoneSameInstant(beijingZone);

        return beijingTime.format(DATE_TIME_FORMATTER);
    }

    /**
     * 获取 2000-01-01 00:00:00 - 2000-01-01 23:59:59 格式的日期
     * @return
     */
    public static String getTodayRange() {
        // 开始时间
        LocalDateTime start = LocalDate.now().atStartOfDay();
        String startTime = start.format(DATE_TIME_FORMATTER);

        // 结束时间
        LocalDateTime end = LocalDate.now().atTime(LocalTime.MAX);
        String endTime = end.format(DATE_TIME_FORMATTER);

        return startTime + " - " + endTime;
    }

    /**
     * 传入2020-01-01
     * 返回2020-01-01 00:00:00的精确到毫秒级别的时间戳
     * @param time
     * @return
     */
    public static Long toEpochMilliByDateStart(String time) {
        LocalDate localDate = LocalDate.parse(time);
        LocalDateTime dateTime = localDate.atStartOfDay();
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
        return zonedDateTime.toInstant().toEpochMilli();
    }

    /**
     * 传入2020-01-01
     * 返回2020-01-01 00:00:00的精确到毫秒级别的时间戳
     * @param time
     * @return
     */
    public static Long toEpochMilliByDateEnd(String time) {
        LocalDate localDate = LocalDate.parse(time);
        LocalDateTime dateTime = localDate.atTime(LocalTime.MAX);
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
        return zonedDateTime.toInstant().toEpochMilli();
    }

    /**
     * 传入2020-01-01 00:00:00
     * 返回2020-01-01 00:00:00的精确到毫秒级别的时间戳
     * @param time
     * @return
     */
    public static Long toEpochMilliBySecond(String time) {
        LocalDateTime dateTime = LocalDateTime.parse(time, DATE_TIME_FORMATTER);
        ZonedDateTime zonedDateTime = dateTime.atZone(ZoneId.systemDefault());
        return zonedDateTime.toInstant().toEpochMilli();
    }



    public static void main(String[] args) {
        Long s = DateUtils.toEpochMilliByDateStart("2023-07-10");
        Long aLong = DateUtils.toEpochMilliByDateEnd("2023-07-10");
        System.out.println(s);
        System.out.println(aLong);
    }
}
